package com.haizhi.crm.biz.service.impl;

import com.haizhi.crm.biz.constant.AchievementType;
import com.haizhi.crm.biz.model.DepositVo;
import com.haizhi.crm.biz.model.LoanVo;
import com.haizhi.crm.biz.model.OpenVo;
import com.haizhi.crm.biz.service.AchievementService;
import com.haizhi.crm.biz.service.CompanyService;
import com.haizhi.crm.biz.util.MessageUtil;
import com.haizhi.crm.common.constant.CrmSchema;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.search.builder.SearchQoBuilder;
import com.haizhi.crm.search.model.SearchVo;
import com.haizhi.crm.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haizhi on 2018/3/6.
 */
@Service
public class AchievementServiceImpl implements AchievementService {

    private static final GLog LOG = new GLog(CompanyService.class);

    @Autowired
    private SearchService searchService;

    @Override
    public Response findDetail(Long userId, AchievementType achievementType) {
        List<Map<String, String>> datas = getSearchData(userId, achievementType);
        if (achievementType == AchievementType.OPEN) {
            return getOpen(datas);
        } else if (achievementType == AchievementType.DEPOSIT) {
            return getDeposit(datas);
        } else {
            return getLoan(datas);
        }
    }

    public Response getOpen(List<Map<String, String>> datas) {
        List<OpenVo> result = new ArrayList<>();
        for (Map<String, String> data : datas) {
            OpenVo openVo = new OpenVo();
            openVo.setCustomerName(data.getOrDefault("customer_name", "" ));
            openVo.setOpenDate(data.getOrDefault("date", "" ));
            result.add(openVo);
        }
        return Response.success(result);
    }

    public Response getLoan(List<Map<String, String>> datas) {
        List<LoanVo> result = new ArrayList<>();
        for (Map<String, String> data : datas) {
            LoanVo loanVo = new LoanVo();
            loanVo.setCustomerName(data.getOrDefault("customer_name", "" ));
            loanVo.setDate(data.getOrDefault("date", "" ));
        }
        return Response.success(result);
    }

    public Response getDeposit(List<Map<String, String>> datas) {
        List<DepositVo> result = new ArrayList<>();
        for (Map<String, String> data : datas) {
            DepositVo depositVo = new DepositVo();
            depositVo.setCustomerName(data.getOrDefault("customer_name", "" ));
            depositVo.setAddMount(data.getOrDefault("mount", "" ));
            depositVo.setBalance(data.getOrDefault("balance", "" ));
            depositVo.setYearDayAvg(data.getOrDefault("daily_avg_balance_in_year", "" ));
            depositVo.setBalance(data.getOrDefault("daily_avg_balance_in_month", "" ));
            result.add(depositVo);
        }
        return Response.success(result);
    }

    public List<Map<String, String>> getSearchData(Long userId, AchievementType achievementType) {
        SearchQoBuilder searchQoBuilder = new SearchQoBuilder();
        searchQoBuilder.setGraph(CrmSchema.CRM);
        searchQoBuilder.addSchemas(CrmSchema.ACCOUNT);
        searchQoBuilder.addTermFilter("customer_manager", userId);
        if (achievementType == AchievementType.OPEN) {
            ;
        } else if (achievementType == AchievementType.DEPOSIT) {
            searchQoBuilder.addTermFilter("product_sub_type", "活期存款", "定期存款", "大额存单" );
        } else {
            searchQoBuilder.addTermFilter("product_sub_type", "一般贷款", "委托贷款", "行外银团贷款（非本行）" );
        }
        SearchVo searchVo = searchService.search(searchQoBuilder.get());
        return MessageUtil.toList(searchVo.getSearchResp().getData());
    }
}
